/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.netflow;

import java.io.*;

public class NetflowLog implements Comparable<NetflowLog> {

    protected int month = -1;

    protected int day = -1;

    protected int year = -1;

    protected int run = -1;

    protected String filePath = "";

    public NetflowLog() {

    }

    public String toString() {
        return filePath + " " + year + "-" + month + "-" + day + " " + run;
    }

    public static NetflowLog generate(File file, File[] files) {
        try {
            // This method attempts to create a log entry from the givn parms
            // File names are of the form flows.YYYYMMDD_HH:MM:SS-0700.gz
            // The files array must be sorted in ascending order

            NetflowLog temp = new NetflowLog();

            String filePath = file.getAbsolutePath();
            String fileName = file.getName();
            String fileRoot = filePath.substring(0, filePath.indexOf("-"));
            String fileRoot2 = filePath.substring(0, filePath.indexOf("_"));
            temp.year = Integer.parseInt(fileName.substring(6, 10));
            temp.month = Integer.parseInt(fileName.substring(10, 12));
            temp.day = Integer.parseInt(fileName.substring(12, 14));
            temp.run = 0;
            int i = 0;
            String tempFileRoot = files[i].getAbsolutePath();
            tempFileRoot = tempFileRoot.substring(0, tempFileRoot.indexOf("_"));
            while (!tempFileRoot.equals(fileRoot2)) {
                i++;

                tempFileRoot = files[i].getAbsolutePath();
                tempFileRoot = tempFileRoot.substring(0, tempFileRoot.indexOf("_"));
            }
            for (; i < files.length; i++) {
                // if (files[i].getAbsolutePath().startsWith(fileRoot)) {
                // }

                temp.run++;
                if (files[i].getAbsolutePath().equals(filePath))
                    break;

            }
            temp.filePath = filePath;
            return temp;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null;
    }

    public static NetflowLog generate(String line) {
        try {
            NetflowLog temp = new NetflowLog();

            // A line should be of the form 'file_path yyyy-mm-dd run'
            String[] tokens = line.split(" ");
            if (tokens.length != 3)
                throw (new Exception("Invalid LFAPEntry format: '" + line + "'"));

            // 1st token is the file path, check that it exists
            temp.filePath = tokens[0];
            File test = new File(temp.filePath);
            if (!test.exists()) {
                File zipTest = new File(temp.filePath + ".gz");
                if (!zipTest.exists())
                    throw (new Exception("File in entry does not exist: '" + temp.filePath + "'"));
                else
                    temp.filePath += ".gz";
            }

            // 2nd token is a date of format yyyy-mm-dd
            String[] dateTokens = tokens[1].split("-");
            if (dateTokens.length != 3)
                throw (new Exception("Invalid LFAPEntry date format: '" + tokens[1] + "'"));
            temp.year = Integer.parseInt(dateTokens[0]);
            temp.month = Integer.parseInt(dateTokens[1]);
            temp.day = Integer.parseInt(dateTokens[2]);

            // 3rd token is the run number
            temp.run = Integer.parseInt(tokens[2]);

            return temp;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null;
    }

    public String getDateStr() {
        String temp = "" + year;
        if (month < 10)
            temp += "0";
        temp += "" + month;
        if (day < 10)
            temp += "0";
        temp += "" + day;
        return temp;
    }

    public int compareTo(NetflowLog log) {
        String compareStr = "" + year;
        if (month < 10)
            compareStr += "0";
        compareStr += "" + month;
        if (day < 10)
            compareStr += "0";
        compareStr += "" + day;
        String runStr = "" + run;
        while (runStr.length() < 4)
            runStr = "0" + runStr;
        compareStr += runStr;

        String compareStr2 = "" + log.year;
        if (log.month < 10)
            compareStr2 += "0";
        compareStr2 += "" + log.month;
        if (log.day < 10)
            compareStr2 += "0";
        compareStr2 += "" + log.day;
        runStr = "" + log.run;
        while (runStr.length() < 4)
            runStr = "0" + runStr;
        compareStr2 += runStr;

        return compareStr.compareTo(compareStr2);
    }

} // End LFAP Log class
